import Vue from 'vue'
import protal from '../api/protal.js'
import {
	webUrl
} from '@/api/base.js'
import {
	base
} from '../api/base.js'
import JSEncrypt from '../static/jsencrypt.min.js'
// #ifdef H5
import wx from "../static/jweixin-1.2.0.js"
// #endif
const appid = 'wx4ccb0b87709a9392'
import {
	getTimestamp
} from '../api/order.js'

Vue.mixin({
	data() {
		return {
			En: '',
			isPc: '',
			zd: '',
			memberList: '',
			phone: '',
			isWx: '',
		}
	},
	onShow() {
		// document.title = 'INCLUSION'
		// if (this.isAndroid_ios()) {
		// 	this.loadShareParam()
		// }
		uni.hideTabBar()
		this.isWx = this.getWx()
	},
	watch: {
		'$store.state': {
			handler(v) {
				this.En = v.lang == 'en' ? 'En' : ''
				this.zd = v.zd
				this.memberList = v.memberList
				this.phone = v.phone
			},
			deep: true,
			immediate: true
		}
	},
	methods: {
		linkWeb(url) {
			if (url.includes('http')) {
				uni.navigateTo({
					url: `/pages/webview/webview?url=${encodeURIComponent(url + '?fromWechat=1')}`
				})
				return
			}
			if (uni.getStorageSync('token')) {
				uni.navigateTo({
					url: `/pages/webview/webview?url=${encodeURIComponent(webUrl + url + '?fromWechat=1&token=' + uni.getStorageSync('token'))}`
				})
				return
			}
			uni.navigateTo({
				url: `/pages/webview/webview?url=${encodeURIComponent(webUrl + url + '?fromWechat=1')}`
			})
		},
		zdToLabel(type, value) {
			if (this.zd[type]) {
				const zdJson = this.zd[type].find(item => item.value == value);
				if (zdJson) {
					return zdJson['text' + this.En]
				}
			}
			return value;
		},
		jsAPI() {

		},
		pageView() {

		},
		day(v) {
			if (v.includes('09-10')) {
				return !this.En ? '9月10日' : '09-10'
			}
			if (v.includes('09-11')) {
				return !this.En ? '9月11日' : '09-11'
			}
			if (v.includes('09-12')) {
				return !this.En ? '9月12日' : '09-12'
			}
			if (v.includes('09-13')) {
				return !this.En ? '9月13日' : '09-13'
			}
		},
		week(v) {
			if (v.includes('09-10')) {
				return !this.En ? '周三' : 'Wednesday'
			}
			if (v.includes('09-11')) {
				return !this.En ? '周四' : 'Thursday'
			}
			if (v.includes('09-12')) {
				return !this.En ? '周五' : 'Friday'
			}
			if (v.includes('09-13')) {
				return !this.En ? '周六' : 'Saturday'
			}
		},
		getPersonDetail(that) {
			return protal.certDetail().then(res => {
				that.userInfo = res.data
			})
		},
		getWx() {
			// #ifdef H5
			var ua = navigator.userAgent.toLowerCase();
			var isWeixin = ua.indexOf('micromessenger') != -1;
			if (isWeixin) {
				return true;
			} else {
				return false;
			}
			// #endif
			// #ifndef H5
			return false
			// #endif
		},
		// 分享
		loadShareParam() {
			if (!this.isWx && !location.href.includes('up.worldaic.com.cn')) return
			let href = !this.isAndroid_ios() ? window.initHref : location.href
			// let href = location.href

			protal.queryWeChatShare({
				linkUrl: href
			}).then(res => {
				if (res.code === '0000') {
					res.data.href = href
					this.loadWxConfig(res.data)
				}
			})
		},
		isAndroid_ios() {
			var u = navigator.userAgent,
				app = navigator.appVersion;
			var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
			var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
			return isAndroid == true ? true : false;
		},
		loadWxConfig({
			appId,
			timestamp,
			nonceStr,
			signature,
			href
		}) {
			wx.config({
				debug: false,
				appId: appId,
				timestamp: timestamp,
				nonceStr: nonceStr,
				signature: signature,
				jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline']
			});

			this.handleShare(href)
		},
		handleShare(href) {
			const that = this
			wx.ready(() => {
				//自定义“分享给朋友”及“分享到QQ”按钮的分享内容（1.4.0）
				// wx.onMenuShareAppMessage({
				// 	title: 'WAIC UP!', // 分享标题
				// 	desc: '《WAIC UP! 》唤醒而生｜即刻解锁你的「AI时代进化指南」', // 分享描述
				// 	link: href, // 分享链接
				// 	imgUrl: 'https://static.worldaic.com.cn/news/images/share-img.png' + this
				// 		.randomParams(), // 分享图标
				// 	success: function() {
				// 		// 用户确认分享后执行的回调函数
				// 		// that.pageShare()
				// 	},
				// 	cancel: function() {
				// 		// 用户取消分享后执行的回调函数
				// 	}
				// });
				// //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容（1.4.0）
				// wx.onMenuShareTimeline({
				// 	title: 'WAIC UP!', // 分享标题
				// 	link: href, // 分享链接
				// 	imgUrl: 'https://static.worldaic.com.cn/news/images/share-img.png' + this
				// 		.randomParams(), // 分享图标
				// 	success: function() {
				// 		// 用户确认分享后执行的回调函数
				// 		// that.pageShare()
				// 	},
				// 	cancel: function() {
				// 		// 用户取消分享后执行的回调函数
				// 	}
				// });

			});
		},
		randomParams() {
			return '?t=' + (new Date().getTime())
		},
		changeLang() {
			if (uni.getLocale() == 'zh-Hans') {
				uni.setLocale('en')
				this.$i18n.locale = 'en'
				this.$store.commit('setLang', 'en')
			} else {
				uni.setLocale('zh-Hans')
				this.$i18n.locale = 'zh-Hans'
				this.$store.commit('setLang', 'zh')
			}
		},
		replacePage(url) {
			uni.navigateTo({
				url
			})
		},
		backPage() {
			const arr = getCurrentPages()
			if (arr.length == 1 || arr[arr.length - 1].route == arr[arr.length - 2].route) {
				uni.redirectTo({
					url: '/pages/index/index'
				})
				return
			}
			uni.navigateBack()
		},
		acceptHuimian(uuid, fn) {
			protal.offlineupdateStatus(uuid, 1).then(res => {
				if (res.code == '0000') {
					fn()
				}
			})
		},
		rejectHuimian(uuid, fn) {
			protal.offlineupdateStatus(uuid, 2).then(res => {
				if (res.code == '0000') {
					fn()
				}
			})
		},
		closeMenu() {
			this.$store.commit('setShowMenu', false)
		}
	}
})